﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NetworkSimulator
{
    public class MIRA : Routing
    {
        private int alpha;

        public int Alpha
        {
            set { this.alpha = value; }
        }

        public MIRA() : base()
        {
            this.alpha = 1;
        }

        /*******************************************************************
         *  MIRA (MINIMUM INTERFERENCE ROUTING ALGORITHM)
         *  
         *  CODER:  HOANG CHUNG HIEN
         *  DATE:   28/3/2013
         *  VERSION: 1.0
         *  
         *******************************************************************/
        public override Path  getPath(Request req)
        {
            this.Renew();

            // If had at least one path from source to destination
            if (BFS(nodes[req.Source], nodes[req.Destination]).Links.Count > 0)
            {
                // Generate link weight
                GenerateMIRACost(nodes[req.Source], nodes[req.Destination]);

                // Remove links with less bandwidth than request bandwidth
                this.removeLinks(req.Bandwidth);

                // Use Dijkstra to get path
                return dijkstra(nodes[req.Source], nodes[req.Destination]);
            }
            return new Path();
        }
        
        // Generate Link cost for MIRA
        private void GenerateMIRACost(Node source, Node dest)
        {
            //int count = 0;
            List<Link> critical_links = new List<Link>();
            
            foreach (IngressEgress ie in ieList.IngressEgressList)
                // if have as least a way from s' to d'
                if (BFS(ie.Ingress, ie.Egress).Links.Count > 0)
                {
                    // Get critical links
                    critical_links = getCriticalLinks(ie.Ingress, ie.Egress);
                    foreach (Link link in critical_links)
                    {
                        cost[link] += alpha;
                    }
                }

        }
    }
}
